package org.jeecg.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.jeecg.domain.*;
import org.jeecg.domain.dto.ServeProviderDto;
import org.jeecg.domain.dto.ServeProviderRenZhengDto;
import org.jeecg.domain.vo.ServeProviderInfoVo;
import org.jeecg.domain.vo.ServeProviderVo;
import org.jeecg.service.*;
import org.jeecg.mapper.ServeProviderMapper;
import org.jeecg.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author 86182
* @description 针对表【serve_provider(服务人员/机构表)】的数据库操作Service实现
* @createDate 2024-12-24 14:48:17
*/
@Service
public class ServeProviderServiceImpl extends ServiceImpl<ServeProviderMapper, ServeProvider>
    implements ServeProviderService{
    @Resource
    private ServeProviderMapper serveProviderMapper;
    @Autowired
    private InstitutionStaffService institutionStaffService;
    @Autowired
    private ServeProviderSettingsService serveProviderSettingsService;
    @Autowired
    private WorkerCertificationService workerCertificationService;
    @Autowired
    private  ServeProviderSyncService serveProviderSyncService;
    @Autowired
    private ServeSkillService serveSkillService;
    //    服务人员列表姓名模糊查询，电话模糊查询，状态，接单
    @Override
    public ResponseResult serveProviderList(ServeProviderDto serveProviderDto) {
        PageHelper.startPage(serveProviderDto.getPageNum(),serveProviderDto.getPageSize());
      List<ServeProviderVo>  serveProviderVoList=serveProviderMapper.serveProviderList(serveProviderDto);
        PageInfo<ServeProviderVo> list = new PageInfo<>(serveProviderVoList);
        return ResponseResult.okResult(list);

//        Page<ServeProviderVo> page = new Page<>(serveProviderDto.getPageNum(), serveProviderDto.getPageSize());
//        LambdaQueryWrapper<ServeProvider> wrapper = Wrappers.<ServeProvider>lambdaQuery().like(StringUtils.checkValNotNull(serveProviderDto.getName()), ServeProvider::getName, serveProviderDto.getName())
//                .like(StringUtils.checkValNotNull(serveProviderDto.getPhone()), ServeProvider::getPhone, serveProviderDto.getPhone())
//                .eq(StringUtils.checkValNotNull(serveProviderDto.getStatus()), ServeProvider::getStatus, serveProviderDto.getStatus())
//                .eq(StringUtils.checkValNotNull(serveProviderDto.getCanPickUp()), ServeProvider::getCanPickUp, serveProviderDto.getCanPickUp())
//                .eq(ServeProvider::getType,2);
//
//        this.page(page, wrapper);
//        List<ServeProvider> records = page.getRecords();
//        for (ServeProvider record : records) {
//            身份证号
//            InstitutionStaff one = institutionStaffService.getOne(Wrappers.<InstitutionStaff>lambdaQuery().eq(InstitutionStaff::getInstitutionId, record.getId()));
//            if (one != null){
//                record.setIdCardNo(one.getIdCardNo());
//            }
//            服务城市
//            ServeProviderSettings one1 = serveProviderSettingsService.getOne(Wrappers.<ServeProviderSettings>lambdaQuery().eq(ServeProviderSettings::getId, record.getId()));
//            if (one1 != null){
//                record.setCityName(one1.getCityName());
////                接单状态
//                record.setCanPickUp(one1.getCanPickUp());
//            }
////            身份证号   认证时间
//            WorkerCertification one = workerCertificationService.getOne(Wrappers.<WorkerCertification>lambdaQuery().eq(WorkerCertification::getId, record.getId()));
//            if (one != null){
//                record.setIdCardNo(one.getIdCardNo());
//                record.setCertificationTime(one.getCertificationTime());
//            }
//        }

//        return ResponseResult.okResult(page);
    }
//解冻
    @Override
    public ResponseResult jiedong(Long id) {
        ServeProvider serveProvider = new ServeProvider();
        serveProvider.setId(id);
        serveProvider.setStatus(0);
        boolean b = this.updateById(serveProvider);
        return  b?ResponseResult.okResult("解冻成功"):ResponseResult.errorResult(500,"解冻失败");

    }
    //    冻结
    @Override
    public ResponseResult dongjie(Long id) {
        ServeProvider serveProvider = new ServeProvider();
        serveProvider.setId(id);
        serveProvider.setStatus(1);
        boolean b = this.updateById(serveProvider);
            return  b?ResponseResult.okResult("冻结成功"):ResponseResult.errorResult(500,"冻结失败");
    }

    //    基本信息
    @Override
    public ResponseResult serveProviderInfo(Long id) {
//        serveProviderMapper.serveProviderInfo(id);
        ServeProviderInfoVo serveProviderInfoVo = new ServeProviderInfoVo();
        ServeProvider provider = this.getById(id);
//        真实姓名
        serveProviderInfoVo.setName(provider.getName());
//        登录账号
        serveProviderInfoVo.setPhone(provider.getPhone());
//        账号状态
        serveProviderInfoVo.setStatus(provider.getStatus());
//        综合评分
        serveProviderInfoVo.setScore(provider.getScore());
        ServeProviderSettings byId = serveProviderSettingsService.getById(provider.getId());
        if (byId != null){
//            服务城市
            serveProviderInfoVo.setCityName(byId.getCityName());
//            服务地址
            serveProviderInfoVo.setIntentionScope(byId.getIntentionScope());
//            接单状态
            serveProviderInfoVo.setCanPickUp(byId.getCanPickUp());
        }
//          认证时间
        WorkerCertification byId1 = workerCertificationService.getById(provider.getId());
        if (byId1 != null){
            serveProviderInfoVo.setCertificationTime(byId1.getCertificationTime());
        }
//        服务分类
        List<ServeSkill> skillList = serveSkillService.list(Wrappers.<ServeSkill>lambdaQuery().eq(ServeSkill::getServeProviderId, provider.getId()));
        ArrayList<String> objects = new ArrayList<>();
        for (ServeSkill serveSkill : skillList) {
            objects.add(serveSkill.getServeTypeName());
        }
        serveProviderInfoVo.setServeTypeName(objects);
// 服务技能
        ServeProviderSync sync = serveProviderSyncService.getById(provider.getId());
        if (sync != null) {
            serveProviderInfoVo.setServeItemIds(sync.getServeItemIds());
        }

        Object serveItemIds = serveProviderInfoVo.getServeItemIds();
        if (serveItemIds != null) {
            String s = JSON.toJSONString(serveItemIds);
            System.out.println("serveItemIds JSON string: " + s); // 调试输出

            List<String> strings = new ArrayList<>();
            if (s.startsWith("[")) {
                // 如果是数组格式，直接解析
                strings = JSON.parseArray(s, String.class);
            } else {
                // 如果不是数组格式，尝试处理为单个元素
                strings.add(s.trim().replaceAll("\"", ""));
            }

            for (String string : strings) {
                List<ServeSkill> list = serveSkillService.list(Wrappers.<ServeSkill>lambdaQuery().eq(ServeSkill::getServeItemId, string));
                for (ServeSkill serveSkill : list) {
                    strings.add(serveSkill.getServeItemName());
                }
            }
            serveProviderInfoVo.setServeItemIds(strings);
        }

        return ResponseResult.okResult(serveProviderInfoVo);

    }

}




